package com.hl.service.impl;

import com.hl.domain.ChatMessageDO;
import com.hl.mapper.ChatMessageMapper;
import com.hl.service.ChatMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ChatMessageServiceImpl implements ChatMessageService {

    @Resource
    private ChatMessageMapper chatMessageMapper;

    @Override
    public void saveIncoming(String messageId, String from, String to, String content, int contentType) {
        ChatMessageDO m = new ChatMessageDO();
        m.setMessageId(messageId != null ? messageId : UUID.randomUUID().toString().replace("-", ""));
        m.setFromUser(from);
        m.setToUser(to);
        m.setContent(content);
        m.setContentType(contentType);
        m.setStatus(0);
        chatMessageMapper.insert(m);
    }

    @Override
    public List<ChatMessageDO> listHistory(String me, String peer, Date before, int pageSize) {
        if (before == null) before = new Date();
        if (pageSize <= 0 || pageSize > 200) pageSize = 50;
        List<ChatMessageDO> desc = chatMessageMapper.listHistory(me, peer, before, pageSize);
        Collections.reverse(desc);
        return desc;
    }

    @Override
    public void markRead(String me, String peer) {
        chatMessageMapper.markRead(me, peer);
    }
}

